

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="运行环境 Android Studio 2020.3.1 @[toc] 1. ListView 与 Spinner组件 1.1 ListView 列表视图 Demo4 Android官网关于ListView的帮助文档：&gt;&gt;传送门&lt;&lt;通过上图可以观察到ListView是View的间接子类。ListView 主要用于显示可垂直滚动的视图集合，其中每个视图都位于列表中上一个视图的">
<meta property="og:type" content="article">
<meta property="og:title" content="Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo">
<meta property="og:url" content="http://example.com/2021/10/25/Android%20_%20%E7%AC%AC2%E7%AB%A0%20_%20ListView%20&%20Adapter%20_%20%E7%9F%A5%E8%AF%86%E7%82%B9%E6%80%BB%E7%BB%93%20_%20+Demo/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="运行环境 Android Studio 2020.3.1 @[toc] 1. ListView 与 Spinner组件 1.1 ListView 列表视图 Demo4 Android官网关于ListView的帮助文档：&gt;&gt;传送门&lt;&lt;通过上图可以观察到ListView是View的间接子类。ListView 主要用于显示可垂直滚动的视图集合，其中每个视图都位于列表中上一个视图的">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/8568f685e5fc428893880dbfc93adeaa.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/8873432fc4b84c8f9118b15682f0b7f9.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b4ee31f10d0c4e68bbd2ce442ade7f05.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4d5973b2233b4299a875d9e91b94ebe1.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a249c66c913b4329a83de343c4033d5b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/bf86f3cb38304806bd4af61a33c078de.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/11a9d3ffe14841b58e9ab02996275829.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/1f16bc1b805e4e579c44eb7603551514.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/17e9f79f96984af694cd3912d76101b8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/8e1a76d3d6e24128b27453cf072c777d.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6eef5acd769d4e6295aee4b68b12cc57.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/5c19999221a84db7b843c4b663de7a03.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d336db8befc648969c132810b333cc87.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9b5db35e773a4a4c9a46acd27c50dc92.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/8f811edd3b2a4ee5bd6fa07b3d89a0f3.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/af86418dd2d84440a0bc5d59ca9f0131.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c53a51928be64fa08bca5a0b4b181d2f.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/28055b5b467244e995c59b60915e0c4e.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/5c05b7330d9643c6bf3b65f3df33b450.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0fa056559bdf4922969be21a9dd83981.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a9a4e64479314dc5b7ffdbfa8e18a15d.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a2b2f39cbd7c499bbda6aaa300c76768.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a426e8e49bd140fbba11a75993bd37ff.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fec446ed6b8b493181aba7f25e868023.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/028600b73b8f41bb9d4f42ce3d54937b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/882657ee6caa4ad7bf5e488365b71d00.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d78402b153444c40b7a4a815667ee26b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e4631886b5274cfe8e89f8d69eec86b6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/71f2ceb821fb47d1b2082bf8977113e8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/593f6f8917a54dd98418fbbaf437fdf5.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/946d7eff1a564e68b682abc1ead109a6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/eb4bde7bd6fa4b10a01d2ba2ac4410c6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/de1b9db8c31f44eb8a63b7277c59727f.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/95965f00b6d8400c851b4093b7b5b4b5.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c0626e4c68764ff5819f03482a1e4b3f.png">
<meta property="article:published_time" content="2021-10-25T03:27:03.000Z">
<meta property="article:modified_time" content="2022-08-22T15:42:04.624Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/8568f685e5fc428893880dbfc93adeaa.png">
  
  
  
  <title>Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-10-25 11:27" pubdate>
          2021年10月25日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          42k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          352 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<p>Android Studio 2020.3.1<br><img src="https://img-blog.csdnimg.cn/8568f685e5fc428893880dbfc93adeaa.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>@[toc]</p>
<h1 id="1-ListView-与-Spinner组件"><a href="#1-ListView-与-Spinner组件" class="headerlink" title="1. ListView 与 Spinner组件"></a>1. ListView 与 Spinner组件</h1><hr>
<h2 id="1-1-ListView-列表视图-Demo4"><a href="#1-1-ListView-列表视图-Demo4" class="headerlink" title="1.1 ListView 列表视图 Demo4"></a>1.1 ListView 列表视图 Demo4</h2><hr>
<p>Android官网关于ListView的帮助文档：<a target="_blank" rel="noopener" href="https://developer.android.google.cn/reference/android/widget/ListView?hl=en">&gt;&gt;传送门&lt;&lt;</a><br><img src="https://img-blog.csdnimg.cn/8873432fc4b84c8f9118b15682f0b7f9.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>通过上图可以观察到<code>ListView</code>是<code>View</code>的间接子类。<br><code>ListView </code>主要用于显示可<code>垂直</code>滚动的视图集合，其中每个视图都位于列表中上一个视图的正下方。</p>
<p><code>ListView</code>是一个未知详细信息的<code>Adapter View</code>，比如类型和内容等信息。</p>
<p><code>ListView</code> 根据需要从<code>List Adapter</code>请求视图，例如在用户上下滚动时显示新视图。<br>（其中Adapter可以使用Android自带的比如ArrayAdapter、SimpleAdapter，除此之外也可以使用继承的类）<br>范例：继承BaseAdapter类实现MyAdapter<br><code>代码模板：</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">BaseAdapter</span> &#123;<br><br>      <span class="hljs-comment">// override other abstract methods here</span><br><br>      <span class="hljs-meta">@Override</span><br>      <span class="hljs-keyword">public</span> View <span class="hljs-title function_">getView</span><span class="hljs-params">(<span class="hljs-type">int</span> position, View convertView, ViewGroup container)</span> &#123;<br>          <span class="hljs-keyword">if</span> (convertView == <span class="hljs-literal">null</span>) &#123;<br>              convertView = getLayoutInflater().inflate(R.layout.list_item, container, <span class="hljs-literal">false</span>);<br>          &#125;<br><br>          ((TextView) convertView.findViewById(android.R.id.text1))<br>                  .setText(getItem(position));<br>          <span class="hljs-keyword">return</span> convertView;<br>      &#125;<br>  &#125;<br></code></pre></td></tr></table></figure>
<p>ListView还有许多方法，这里只是简单入门，想知道更多请参考上面官网的帮助文档。</p>
<h2 id="1-2-Spinner-下拉列表-Demo1"><a href="#1-2-Spinner-下拉列表-Demo1" class="headerlink" title="1.2 Spinner 下拉列表 Demo1"></a>1.2 Spinner 下拉列表 Demo1</h2><hr>
<p>参考资料：<a target="_blank" rel="noopener" href="https://developer.android.google.cn/reference/android/widget/Spinner?hl=en">&gt;&gt;传送门&lt;&lt;</a><br><img src="https://img-blog.csdnimg.cn/b4ee31f10d0c4e68bbd2ce442ade7f05.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>Spinner</code>是一次显示一个子对象并允许用户在其中进行选择的视图。微调器中的项目来自与此视图关联的适配器。<br><code>Spinner</code>内的项目来源于其<code>Adapter</code>相关的<code>View</code></p>
<h2 id="1-3-ListView与Spinner的区别"><a href="#1-3-ListView与Spinner的区别" class="headerlink" title="1.3 ListView与Spinner的区别"></a>1.3 ListView与Spinner的区别</h2><hr>
<p>相同点</p>
<ul>
<li>都是<code>View</code>的间接子类</li>
<li>都是展示一组数据的组件</li>
<li>都可以使用继承的Adapter类</li>
</ul>
<p>不同点</p>
<ul>
<li>ListView的项目会直接显示出来，而Spinner则是归纳到同一个组件，类似按钮，需要点击后才会显示所有的项目。</li>
<li>ListView适合购物商品、视频课程等这类的布局，而Spinner适合表单的填写，比如选择所在地区。</li>
</ul>
<h1 id="2-对象转换"><a href="#2-对象转换" class="headerlink" title="2. 对象转换"></a>2. 对象转换</h1><hr>
<h2 id="2-1-将XML配置文件的内容转化为Java对象-Demo2"><a href="#2-1-将XML配置文件的内容转化为Java对象-Demo2" class="headerlink" title="2.1 将XML配置文件的内容转化为Java对象 Demo2"></a>2.1 将XML配置文件的内容转化为Java对象 Demo2</h2><hr>
<p><img src="https://img-blog.csdnimg.cn/4d5973b2233b4299a875d9e91b94ebe1.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>比如获取string类型的数据<br><code>colors.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;purple_200&quot;</span>&gt;</span>#FFBB86FC<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;purple_500&quot;</span>&gt;</span>#FF6200EE<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;purple_700&quot;</span>&gt;</span>#FF3700B3<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teal_200&quot;</span>&gt;</span>#FF03DAC5<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teal_700&quot;</span>&gt;</span>#FF018786<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;black&quot;</span>&gt;</span>#FF000000<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;white&quot;</span>&gt;</span>#FFFFFFFF<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testColors&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>#f00<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>#0f0<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>#00f<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">array</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">string-array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testColorNames&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Red<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Green<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Blue<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">string-array</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span><br></code></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Resources</span> <span class="hljs-variable">resources</span> <span class="hljs-operator">=</span> getResources();<br>String[] colorNames = resources.getStringArray(R.array.testColorNames);<br></code></pre></td></tr></table></figure>

<h2 id="2-2-将组件转化为Java对象"><a href="#2-2-将组件转化为Java对象" class="headerlink" title="2.2 将组件转化为Java对象"></a>2.2 将组件转化为Java对象</h2><hr>
<p>方法：<code>findViewById(R.id.xxx)</code><br>范例：<code>Button button = findViewById(R.id.myButton);</code></p>
<h1 id="3-TypedArray"><a href="#3-TypedArray" class="headerlink" title="3. TypedArray"></a>3. TypedArray</h1><hr>
<p>参考资料：<a target="_blank" rel="noopener" href="https://developer.android.google.cn/reference/android/content/res/TypedArray?hl=en">&gt;&gt;传送门&lt;&lt;</a><br><img src="https://img-blog.csdnimg.cn/a249c66c913b4329a83de343c4033d5b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>官方解释：</p>
<blockquote>
<p>Container for an array of values that were retrieved with Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) or Resources#obtainAttributes. Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.</p>
</blockquote>
<p>意思大概就是该对象用来读取XML文件里的配置，比如通过Java代码去控制APP里某个组件的字体大小等。</p>
<h2 id="3-1-创建对象"><a href="#3-1-创建对象" class="headerlink" title="3.1 创建对象"></a>3.1 创建对象</h2><hr>
<p><code>dimens.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testDimens&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>12sp<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>16sp<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>20sp<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">array</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">string-array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testDimenNames&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Small<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Medium<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Big<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">string-array</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span><br></code></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">Resources</span> <span class="hljs-variable">resources</span> <span class="hljs-operator">=</span> getResources();<br>String[] dimenNames= resources.getStringArray(R.array.testDimenNames);<br><span class="hljs-type">TypedArray</span> <span class="hljs-variable">dimens</span> <span class="hljs-operator">=</span> resources.obtainTypedArray(R.array.testDimens);<br></code></pre></td></tr></table></figure>
<h2 id="3-2-常用方法"><a href="#3-2-常用方法" class="headerlink" title="3.2 常用方法"></a>3.2 常用方法</h2><hr>
<p><img src="https://img-blog.csdnimg.cn/bf86f3cb38304806bd4af61a33c078de.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>范例：获取字体大小的属性</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">float</span> <span class="hljs-variable">dim</span> <span class="hljs-operator">=</span> dimens.getDimension(i, <span class="hljs-number">10.0f</span>);<br></code></pre></td></tr></table></figure>
<h1 id="4-监听事件"><a href="#4-监听事件" class="headerlink" title="4. 监听事件"></a>4. 监听事件</h1><hr>
<h2 id="4-1-Spinner-监听事件"><a href="#4-1-Spinner-监听事件" class="headerlink" title="4.1 Spinner 监听事件"></a>4.1 Spinner 监听事件</h2><hr>
<p><code>下拉列表选择第 i 个项时的监听事件：Demo1</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java">ArrayAdapter&lt;String&gt; adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_single_choice, cities);<br>sp.setAdapter(adapter);<br>sp.setOnItemSelectedListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemSelectedListener() &#123;<br>		<span class="hljs-meta">@Override</span><br>		<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>		<span class="hljs-type">String</span> <span class="hljs-variable">city</span> <span class="hljs-operator">=</span> cities[i];<br>&#125;<br><br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onNothingSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView)</span> &#123;&#125;<br>&#125;);<br></code></pre></td></tr></table></figure>
<h2 id="4-2-ListView-监听事件"><a href="#4-2-ListView-监听事件" class="headerlink" title="4.2 ListView 监听事件"></a>4.2 ListView 监听事件</h2><hr>
<p><code>当列表的某个项被单击时</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java">lv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>	<span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>		<br>    &#125;<br>&#125;);<br></code></pre></td></tr></table></figure>

<h2 id="4-3-ImageView-监听事件"><a href="#4-3-ImageView-监听事件" class="headerlink" title="4.3 ImageView 监听事件"></a>4.3 ImageView 监听事件</h2><hr>
<p><code>图片被单击时</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java">iv_call.setOnClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">View</span>.OnClickListener() &#123;<br>	<span class="hljs-meta">@Override</span><br>	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onClick</span><span class="hljs-params">(View view)</span> &#123;<br>	Toast.makeText(context, <span class="hljs-string">&quot;图片被单击!&quot;</span>,Toast.LENGTH_LONG).show();<br>	&#125;<br>&#125;);<br></code></pre></td></tr></table></figure>
<h1 id="5-Adapter"><a href="#5-Adapter" class="headerlink" title="5. Adapter"></a>5. Adapter</h1><hr>
<p>参考资料：<a target="_blank" rel="noopener" href="https://developer.android.google.cn/reference/kotlin/android/widget/Adapter?hl=en">&gt;&gt;传送门&lt;&lt;</a><br><img src="https://img-blog.csdnimg.cn/11a9d3ffe14841b58e9ab02996275829.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>Adapter是将数据绑定到UI界面上的桥接类。</li>
<li>Adapter负责创建显示每个项目的子View和提供对下层数据的访问。</li>
<li>Adapter充当Adapter View和该视图的基础数据之间的桥梁。</li>
<li>Adapter负责为数据集中的每个项目生成android.view.View。</li>
</ul>
<h2 id="5-1-ArrayAdapter-Demo1"><a href="#5-1-ArrayAdapter-Demo1" class="headerlink" title="5.1 ArrayAdapter Demo1"></a>5.1 ArrayAdapter Demo1</h2><hr>
<p>参考资料：<a target="_blank" rel="noopener" href="https://developer.android.google.cn/reference/android/widget/ArrayAdapter?hl=en">&gt;&gt;传送门&lt;&lt;</a><br><img src="https://img-blog.csdnimg.cn/1f16bc1b805e4e579c44eb7603551514.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>构造方法：<br><img src="https://img-blog.csdnimg.cn/17e9f79f96984af694cd3912d76101b8.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>范例：创建一个ArrayAdapter对象</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java">String[] cities = resources.getStringArray(R.array.cities);<br>ArrayAdapter&lt;String&gt; adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_single_choice, cities);<br></code></pre></td></tr></table></figure>
<p>官方对ArrayAdapter的解释：</p>
<blockquote>
<p>You can use this adapter to provide views for an AdapterView, Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner.</p>
</blockquote>
<blockquote>
<p>By default, the array adapter creates a view by calling Object#toString() on each data object in the collection you provide, and places the result in a TextView. You may also customize(定制) what type of view is used for the data object in the collection. To customize what type of view is used for the data object, override getView(int, android.view.View, android.view.ViewGroup) and inflate a view resource.</p>
</blockquote>
<p>大概意思是说Adapter可以用来提供视图，并且可以与基于列表的用户界面小部件（如ListView或Spinner）一起使用。则有：<br><code> spinner.setAdapter(myArrayAdapter);</code><br><code>ListView.setAdapter(myArrayAdapter);</code><br>如果要使用自定义的ArrapAdapter的话，需要继承该类并重写<code>getView(int, android.view.View, android.view.ViewGroup) </code>方法。</p>
<h2 id="5-2-SimpleAdapter-Demo5"><a href="#5-2-SimpleAdapter-Demo5" class="headerlink" title="5.2 SimpleAdapter Demo5"></a>5.2 SimpleAdapter Demo5</h2><hr>
<p>参考资料：<a target="_blank" rel="noopener" href="https://developer.android.google.cn/reference/android/widget/SimpleAdapter?hl=en">&gt;&gt;传送门&lt;&lt;</a></p>
<p><img src="https://img-blog.csdnimg.cn/8e1a76d3d6e24128b27453cf072c777d.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li><code>SimpleAdapter</code>是将静态数据映射到XML文件中定义的视图的简单适配器。</li>
<li>可以将支持列表的数据指定为map的ArrayList。</li>
<li>ArrayList中的每个条目对应于列表中的一行。</li>
<li><blockquote>
<p>You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views. </p>
</blockquote>
</li>
<li>上面这句来自官方的解释，意思是可以指定定义用于显示行的视图的XML文件，以及从映射中的键到特定视图的映射。即相当于不同布局的嵌套使用比如：<br><img src="https://img-blog.csdnimg.cn/6eef5acd769d4e6295aee4b68b12cc57.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>类似于通讯录的界面，每一个联系人都有图像 + 名称 + 电话号码 + 呼叫按钮的布局，而所有的联系人又在同一个ListView组件中。</li>
</ul>
<p>构造方法：<br><img src="https://img-blog.csdnimg.cn/5c19999221a84db7b843c4b663de7a03.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>范例：创建 SimpleAdapter对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java">String[] names = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[] &#123;<span class="hljs-string">&quot;周星驰&quot;</span>, <span class="hljs-string">&quot;彭于晏&quot;</span>, <span class="hljs-string">&quot;买瓜的强子&quot;</span>&#125;;<br>String []phones = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[] &#123;<span class="hljs-string">&quot;666&quot;</span>, <span class="hljs-string">&quot;520&quot;</span>, <span class="hljs-string">&quot;999&quot;</span>&#125;;<br><span class="hljs-type">int</span> [] picIds = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span> [] &#123;R.drawable.zhouxingchi, R.drawable.pengyuyan, R.drawable.liuhuaqiang&#125;;<br>ArrayList&lt;HashMap&lt;String, Object&gt;&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; names.length; i++) &#123;<br>	HashMap&lt;String, Object&gt; hashMap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>	hashMap.put(KEY_NAME, names[i]);<br>	hashMap.put(KEY_PHONE, phones[i]);<br>	hashMap.put(KEY_PIC_ID, picIds[i]);<br>	list.add(hashMap);<br>&#125;<br><span class="hljs-type">SimpleAdapter</span> <span class="hljs-variable">adapter</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleAdapter</span>(<br><span class="hljs-built_in">this</span>, <br>list, <br>R.layout.row_view,<br><span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[] &#123;KEY_NAME, KEY_PHONE, KEY_PIC_ID&#125;,<br><span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span> []&#123;R.id.row_view_tv_name, R.id.row_view_tv_phone, R.id.row_view_iv&#125;);<br></code></pre></td></tr></table></figure>

<h2 id="5-3-ArrayAdapter-与-SimpleAdapter-比较"><a href="#5-3-ArrayAdapter-与-SimpleAdapter-比较" class="headerlink" title="5.3 ArrayAdapter 与 SimpleAdapter 比较"></a>5.3 ArrayAdapter 与 SimpleAdapter 比较</h2><hr>
<p>相同点：</p>
<ul>
<li>都可用作组件的Adapter适配器</li>
<li>都支持多元素的容器</li>
</ul>
<p>不同点：</p>
<ul>
<li>ArrayAdapter通常是存储数组型数据如List、Array等，而SimpleAdapter则用HashMap型数据</li>
</ul>
<h2 id="5-4-继承Adapter类的代码框架"><a href="#5-4-继承Adapter类的代码框架" class="headerlink" title="5.4 继承Adapter类的代码框架"></a>5.4 继承Adapter类的代码框架</h2><p>以继承ArrayAdapter类为例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PictureAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;Picture&gt; &#123;<br>    <span class="hljs-keyword">private</span> Context context;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;Picture&gt; list;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">PictureAdapter</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> Context context, ArrayList&lt;Picture&gt; list)</span> &#123;<br>        <span class="hljs-built_in">super</span>(context, android.R.layout.simple_list_item_1, list);<br>        <span class="hljs-built_in">this</span>.context = context;<br>        <span class="hljs-built_in">this</span>.list = list;<br>    &#125;<br><br>    <span class="hljs-meta">@NonNull</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> View <span class="hljs-title function_">getView</span><span class="hljs-params">(<span class="hljs-type">int</span> position, <span class="hljs-meta">@Nullable</span> View convertView, <span class="hljs-meta">@NonNull</span> ViewGroup parent)</span> &#123;<br>        View v;<br>        <span class="hljs-keyword">if</span>(convertView == <span class="hljs-literal">null</span>)&#123;<br>            v = LayoutInflater.from(context).inflate(R.layout.row_view, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>);<br>        &#125; <span class="hljs-keyword">else</span>&#123;<br>            v = convertView;<br>        &#125;<br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_iv);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_tv);<br>        <span class="hljs-type">Picture</span> <span class="hljs-variable">picture</span> <span class="hljs-operator">=</span> list.get(position);<br>        iv.setImageResource(picture.getPicId());;<br>        tv.setText(picture.getName());<br>        <span class="hljs-keyword">return</span> v;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<h1 id="6-Demo"><a href="#6-Demo" class="headerlink" title="6. Demo"></a>6. Demo</h1><hr>
<h2 id="6-1-实现一个可选择的列表框"><a href="#6-1-实现一个可选择的列表框" class="headerlink" title="6.1 实现一个可选择的列表框"></a>6.1 实现一个可选择的列表框</h2><hr>
<p><code>实现效果</code></p>
<p><img src="https://img-blog.csdnimg.cn/d336db8befc648969c132810b333cc87.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/9b5db35e773a4a4c9a46acd27c50dc92.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>layout_main.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    &gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/tv_result&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;TextView&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">Spinner</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/spinner&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>strings.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">string</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;app_name&quot;</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">string</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">string-array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;cities&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Wenzhou<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Hangzhou<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Ningbo<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">string-array</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><span class="hljs-keyword">import</span> android.content.res.Resources;<br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.Spinner;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br>        <span class="hljs-type">Resources</span> <span class="hljs-variable">resources</span> <span class="hljs-operator">=</span> getResources();<br>        String[] cities = resources.getStringArray(R.array.cities);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> findViewById(R.id.tv_result);<br>        <span class="hljs-type">Spinner</span> <span class="hljs-variable">sp</span> <span class="hljs-operator">=</span> findViewById(R.id.spinner);<br>        ArrayAdapter&lt;String&gt; adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_single_choice, cities);<br>        sp.setAdapter(adapter);<br>        sp.setOnItemSelectedListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemSelectedListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                <span class="hljs-type">String</span> <span class="hljs-variable">city</span> <span class="hljs-operator">=</span> cities[i];<br>                tv.setText(city);<br>            &#125;<br><br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onNothingSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView)</span> &#123;&#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-2-实现通过列表框修改文本的颜色"><a href="#6-2-实现通过列表框修改文本的颜色" class="headerlink" title="6.2 实现通过列表框修改文本的颜色"></a>6.2 实现通过列表框修改文本的颜色</h2><hr>
<p><code>实现效果</code></p>
<p><img src="https://img-blog.csdnimg.cn/8f811edd3b2a4ee5bd6fa07b3d89a0f3.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/af86418dd2d84440a0bc5d59ca9f0131.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>layout_main.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/textView&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;Font&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">Spinner</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/spinner&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>colors.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;purple_200&quot;</span>&gt;</span>#FFBB86FC<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;purple_500&quot;</span>&gt;</span>#FF6200EE<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;purple_700&quot;</span>&gt;</span>#FF3700B3<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teal_200&quot;</span>&gt;</span>#FF03DAC5<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teal_700&quot;</span>&gt;</span>#FF018786<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;black&quot;</span>&gt;</span>#FF000000<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">color</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;white&quot;</span>&gt;</span>#FFFFFFFF<span class="hljs-tag">&lt;/<span class="hljs-name">color</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testColors&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>#f00<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>#0f0<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>#00f<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">array</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">string-array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testColorNames&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Red<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Green<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Blue<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">string-array</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><span class="hljs-keyword">import</span> android.content.res.Resources;<br><span class="hljs-keyword">import</span> android.content.res.TypedArray;<br><span class="hljs-keyword">import</span> android.graphics.Color;<br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.Spinner;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br><br>        <span class="hljs-type">Spinner</span> <span class="hljs-variable">sp</span> <span class="hljs-operator">=</span> findViewById(R.id.spinner);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> findViewById(R.id.textView);<br><br>        <span class="hljs-type">Resources</span> <span class="hljs-variable">resources</span> <span class="hljs-operator">=</span> getResources();<br>        String[] colorNames = resources.getStringArray(R.array.testColorNames);<br>        ArrayAdapter&lt;String&gt; adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_single_choice,colorNames);<br>        <span class="hljs-type">TypedArray</span> <span class="hljs-variable">testColors</span> <span class="hljs-operator">=</span> resources.obtainTypedArray(R.array.testColors);<br>        sp.setAdapter(adapter);<br>        sp.setOnItemSelectedListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemSelectedListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                <span class="hljs-type">int</span> <span class="hljs-variable">color</span> <span class="hljs-operator">=</span> testColors.getColor(i, Color.BLACK);<br>                tv.setTextColor(color);<br>            &#125;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onNothingSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView)</span> &#123;&#125;<br>        &#125;);<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-3-实现通过列表框修改文本的字体大小"><a href="#6-3-实现通过列表框修改文本的字体大小" class="headerlink" title="6.3 实现通过列表框修改文本的字体大小"></a>6.3 实现通过列表框修改文本的字体大小</h2><p><code>实现效果</code><br><img src="https://img-blog.csdnimg.cn/c53a51928be64fa08bca5a0b4b181d2f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/28055b5b467244e995c59b60915e0c4e.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>layout_main.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/textView&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;Font&quot;</span></span><br><span class="hljs-tag">        /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">Spinner</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/spinner&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>values/dimens.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">resources</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testDimens&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>12sp<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>16sp<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>20sp<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">array</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">string-array</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;testDimenNames&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Small<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Medium<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>Big<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">string-array</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">resources</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.content.res.Resources;<br><span class="hljs-keyword">import</span> android.content.res.TypedArray;<br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.Spinner;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> findViewById(R.id.textView);<br>        <span class="hljs-type">Spinner</span> <span class="hljs-variable">sp</span> <span class="hljs-operator">=</span> findViewById(R.id.spinner);<br>        <span class="hljs-type">Resources</span> <span class="hljs-variable">resources</span> <span class="hljs-operator">=</span> getResources();<br>        String[] dimenNames= resources.getStringArray(R.array.testDimenNames);<br>        <span class="hljs-type">TypedArray</span> <span class="hljs-variable">dimens</span> <span class="hljs-operator">=</span> resources.obtainTypedArray(R.array.testDimens);<br>        ArrayAdapter&lt;String&gt;adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_1, dimenNames);<br>        sp.setAdapter(adapter);<br>        sp.setOnItemSelectedListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemSelectedListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                <span class="hljs-type">float</span> <span class="hljs-variable">dim</span> <span class="hljs-operator">=</span> dimens.getDimension(i, <span class="hljs-number">10.0f</span>);<br>                tv.setTextSize(dim);<br>            &#125;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onNothingSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView)</span> &#123;<br><br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-4-实现通过列表框切换图片"><a href="#6-4-实现通过列表框切换图片" class="headerlink" title="6.4 实现通过列表框切换图片"></a>6.4 实现通过列表框切换图片</h2><p><code>实现效果</code><br><img src="https://img-blog.csdnimg.cn/5c05b7330d9643c6bf3b65f3df33b450.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/0fa056559bdf4922969be21a9dd83981.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>提前准备好的图片资源：<br><img src="https://img-blog.csdnimg.cn/a9a4e64479314dc5b7ffdbfa8e18a15d.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>layout_main.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/imageView&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;200dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;200dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:scaleType</span>=<span class="hljs-string">&quot;centerCrop&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_gravity</span>=<span class="hljs-string">&quot;center&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@drawable/image1&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ListView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/listview&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><span class="hljs-keyword">import</span> android.widget.ListView;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br>    <span class="hljs-type">int</span>[] picIds = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span> []&#123;R.drawable.image1, R.drawable.image2, R.drawable.image3&#125;;<br>    String[] picNames = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[]&#123;<span class="hljs-string">&quot;图片1&quot;</span>, <span class="hljs-string">&quot;图片2&quot;</span>, <span class="hljs-string">&quot;图片3&quot;</span>&#125;;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br><br>        <span class="hljs-type">ListView</span> <span class="hljs-variable">lv</span> <span class="hljs-operator">=</span> findViewById(R.id.listview);<br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> findViewById(R.id.imageView);<br>        ArrayAdapter&lt;String&gt; adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_1, picNames);<br>        lv.setAdapter(adapter);<br>        lv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                <span class="hljs-type">int</span> <span class="hljs-variable">picId</span> <span class="hljs-operator">=</span> picIds[i];<br>                iv.setImageResource(picId);<br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-5-实现手机通讯录的界面模拟"><a href="#6-5-实现手机通讯录的界面模拟" class="headerlink" title="6.5 实现手机通讯录的界面模拟"></a>6.5 实现手机通讯录的界面模拟</h2><hr>
<p><code>实现效果</code><br><img src="https://img-blog.csdnimg.cn/a2b2f39cbd7c499bbda6aaa300c76768.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>row_view.xml</code><br><img src="https://img-blog.csdnimg.cn/a426e8e49bd140fbba11a75993bd37ff.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;horizontal&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center_vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_iv&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;100dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;100dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:scaleType</span>=<span class="hljs-string">&quot;centerCrop&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@drawable/zhouxingchi&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_weight</span>=<span class="hljs-string">&quot;1&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:paddingLeft</span>=<span class="hljs-string">&quot;10dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center_vertical&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_tv_name&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">&quot;24sp&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;Name&quot;</span> /&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_tv_phone&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;Phone&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_iv_call&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;40dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;40dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:paddingLeft</span>=<span class="hljs-string">&quot;10dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@android:drawable/sym_action_call&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>layout_main.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ListView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/listview&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example;<br><br><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ListView;<br><span class="hljs-keyword">import</span> android.widget.SimpleAdapter;<br><span class="hljs-keyword">import</span> android.widget.Toast;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><span class="hljs-keyword">import</span> java.util.HashMap;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String KEY_NAME=<span class="hljs-string">&quot;key_name&quot;</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String KEY_PHONE=<span class="hljs-string">&quot;key_phone&quot;</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String KEY_PIC_ID=<span class="hljs-string">&quot;key_pic_id&quot;</span>;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br>        <span class="hljs-type">ListView</span> <span class="hljs-variable">lv</span> <span class="hljs-operator">=</span> findViewById(R.id.listview);<br><br>        String[] names = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[] &#123;<span class="hljs-string">&quot;周星驰&quot;</span>, <span class="hljs-string">&quot;彭于晏&quot;</span>, <span class="hljs-string">&quot;买瓜的强子&quot;</span>&#125;;<br>        String []phones = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[] &#123;<span class="hljs-string">&quot;666&quot;</span>, <span class="hljs-string">&quot;520&quot;</span>, <span class="hljs-string">&quot;999&quot;</span>&#125;;<br>        <span class="hljs-type">int</span> [] picIds = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span> [] &#123;R.drawable.zhouxingchi, R.drawable.pengyuyan, R.drawable.liuhuaqiang&#125;;<br>        ArrayList&lt;HashMap&lt;String, Object&gt;&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; names.length; i++) &#123;<br>            HashMap&lt;String, Object&gt; hashMap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>            hashMap.put(KEY_NAME, names[i]);<br>            hashMap.put(KEY_PHONE, phones[i]);<br>            hashMap.put(KEY_PIC_ID, picIds[i]);<br>            list.add(hashMap);<br>        &#125;<br>        <span class="hljs-type">SimpleAdapter</span> <span class="hljs-variable">adapter</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleAdapter</span>(<span class="hljs-built_in">this</span>, list, R.layout.row_view,<br>                <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[] &#123;KEY_NAME, KEY_PHONE, KEY_PIC_ID&#125;,<br>                <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span> []&#123;R.id.row_view_tv_name, R.id.row_view_tv_phone, R.id.row_view_iv&#125;);<br>        lv.setAdapter(adapter);<br>        lv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                HashMap&lt;String, Object&gt; hashMap = list.get(i);<br>                <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> (String) hashMap.get(KEY_NAME);<br>                Toast.makeText(MainActivity.<span class="hljs-built_in">this</span>, name, Toast.LENGTH_LONG).show();<br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-6-实现模拟通讯录里的呼叫功能"><a href="#6-6-实现模拟通讯录里的呼叫功能" class="headerlink" title="6.6 实现模拟通讯录里的呼叫功能"></a>6.6 实现模拟通讯录里的呼叫功能</h2><p><code>实现效果</code><br><img src="https://img-blog.csdnimg.cn/fec446ed6b8b493181aba7f25e868023.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>该Demo的layout和第五个Demo的一样，就不重复Copy了，二者不同点在于这个ListView使用了继承的Adapter适配器类。</p>
<p><img src="https://img-blog.csdnimg.cn/028600b73b8f41bb9d4f42ce3d54937b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>PhoneData.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PhoneData</span> &#123;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> String phone;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> picId;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">PhoneData</span><span class="hljs-params">(String name, String phone, <span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.phone = phone;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getPhone</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> phone;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setPhone</span><span class="hljs-params">(String phone)</span> &#123;<br>        <span class="hljs-built_in">this</span>.phone = phone;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getPicId</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> picId;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setPicId</span><span class="hljs-params">(<span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p><code>PhoneDataAdapter.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> android.content.Context;<br><span class="hljs-keyword">import</span> android.text.TextUtils;<br><span class="hljs-keyword">import</span> android.view.LayoutInflater;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.view.ViewGroup;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><span class="hljs-keyword">import</span> android.widget.Toast;<br><br><span class="hljs-keyword">import</span> androidx.annotation.NonNull;<br><span class="hljs-keyword">import</span> androidx.annotation.Nullable;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PhoneDataAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;PhoneData&gt; &#123;<br>    <span class="hljs-keyword">private</span> Context context;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;PhoneData&gt; list;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">PhoneDataAdapter</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> Context context ,ArrayList&lt;PhoneData&gt; list)</span> &#123;<br>        <span class="hljs-built_in">super</span>(context, android.R.layout.simple_list_item_1, list);<br>        <span class="hljs-built_in">this</span>.context = context;<br>        <span class="hljs-built_in">this</span>.list = list;<br>    &#125;<br><br>    <span class="hljs-meta">@NonNull</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> View <span class="hljs-title function_">getView</span><span class="hljs-params">(<span class="hljs-type">int</span> position, <span class="hljs-meta">@Nullable</span> View convertView, <span class="hljs-meta">@NonNull</span> ViewGroup parent)</span> &#123;<br>        View v;<br>        <span class="hljs-keyword">if</span>(convertView == <span class="hljs-literal">null</span>)&#123;<br>            v = LayoutInflater.from(context).inflate(R.layout.row_view, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            v = convertView;<br>        &#125;<br>        <span class="hljs-type">PhoneData</span> <span class="hljs-variable">phoneData</span> <span class="hljs-operator">=</span> list.get(position);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv_name</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_tv_name);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv_phone</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_tv_phone);<br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_iv);<br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv_call</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_iv_call);<br>        tv_name.setText(phoneData.getName());<br>        tv_phone.setText(phoneData.getPhone());<br>        iv.setImageResource(phoneData.getPicId());<br>        <span class="hljs-keyword">if</span>(TextUtils.isEmpty(phoneData.getPhone()))&#123;<br>            iv_call.setVisibility(View.GONE);<br>        &#125;<br>        iv_call.setOnClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">View</span>.OnClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onClick</span><span class="hljs-params">(View view)</span> &#123;<br>                Toast.makeText(context, <span class="hljs-string">&quot;Calling &quot;</span> + phoneData.getPhone(), Toast.LENGTH_LONG).show();<br>            &#125;<br>        &#125;);<br>        <span class="hljs-keyword">return</span> v;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ListView;<br><span class="hljs-keyword">import</span> android.widget.Toast;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br>        <span class="hljs-type">ListView</span> <span class="hljs-variable">lv</span> <span class="hljs-operator">=</span> findViewById(R.id.listview);<br>        ArrayList&lt;PhoneData&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">PhoneData</span>(<span class="hljs-string">&quot;周星驰&quot;</span>, <span class="hljs-string">&quot;666&quot;</span>, R.drawable.zhouxingchi));<br>        list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">PhoneData</span>(<span class="hljs-string">&quot;彭于晏&quot;</span>, <span class="hljs-string">&quot;520&quot;</span>, R.drawable.pengyuyan));<br>        list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">PhoneData</span>(<span class="hljs-string">&quot;买瓜的强子&quot;</span>, <span class="hljs-string">&quot;999&quot;</span>, R.drawable.liuhuaqiang));<br>        <span class="hljs-type">PhoneDataAdapter</span> <span class="hljs-variable">adapter</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PhoneDataAdapter</span>(<span class="hljs-built_in">this</span>, list);<br>        lv.setAdapter(adapter);<br>        lv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br><span class="hljs-comment">//                PhoneData phoneData = list.get(i);</span><br><span class="hljs-comment">//                PhoneData phoneData = adapter.getItem(i);</span><br>                <span class="hljs-type">PhoneData</span> <span class="hljs-variable">phoneData</span> <span class="hljs-operator">=</span> (PhoneData) lv.getItemAtPosition(i);<br>                Toast.makeText(MainActivity.<span class="hljs-built_in">this</span>, phoneData.getName(), Toast.LENGTH_LONG).show();<br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-7-实现多行多列的列表"><a href="#6-7-实现多行多列的列表" class="headerlink" title="6.7 实现多行多列的列表"></a>6.7 实现多行多列的列表</h2><p><code>实现效果</code><br><img src="https://img-blog.csdnimg.cn/882657ee6caa4ad7bf5e488365b71d00.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>layout_main.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/textView&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;TextView&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">GridView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/gridview&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:numColumns</span>=<span class="hljs-string">&quot;3&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:stretchMode</span>=<span class="hljs-string">&quot;columnWidth&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.GridView;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    ArrayList&lt;String&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>();<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br><br>        <span class="hljs-type">GridView</span> <span class="hljs-variable">gv</span> <span class="hljs-operator">=</span> findViewById(R.id.gridview);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> findViewById(R.id.textView);<br>        <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">99</span>; i++)&#123;<br>            <span class="hljs-type">String</span> <span class="hljs-variable">s</span> <span class="hljs-operator">=</span> String.format(<span class="hljs-string">&quot;Item%02d&quot;</span>, i);<br>            list.add(s);<br>        &#125;<br>        ArrayAdapter&lt;String&gt; adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_1, list);<br>        gv.setAdapter(adapter);<br>        gv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                 <span class="hljs-type">String</span> <span class="hljs-variable">s</span> <span class="hljs-operator">=</span> list.get(i);<br>                 tv.setText(s);<br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-8-实现展示风景的列表框"><a href="#6-8-实现展示风景的列表框" class="headerlink" title="6.8 实现展示风景的列表框"></a>6.8 实现展示风景的列表框</h2><hr>
<p><code>实现效果</code><br><img src="https://img-blog.csdnimg.cn/d78402b153444c40b7a4a815667ee26b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/e4631886b5274cfe8e89f8d69eec86b6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><code>row_view.xml</code><br><img src="https://img-blog.csdnimg.cn/71f2ceb821fb47d1b2082bf8977113e8.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_iv&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;130dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;130dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:scaleType</span>=<span class="hljs-string">&quot;centerCrop&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@drawable/image1&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_tv&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;TextView&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><code>layout_main.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/textView&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;TextView&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">GridView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/gridview&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:numColumns</span>=<span class="hljs-string">&quot;2&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:stretchMode</span>=<span class="hljs-string">&quot;columnWidth&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><code>Picture.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Picture</span> &#123;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> picId;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Picture</span><span class="hljs-params">(String name, <span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getPicId</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> picId;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setPicId</span><span class="hljs-params">(<span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<p><code>PictureAdapter.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> android.content.Context;<br><span class="hljs-keyword">import</span> android.view.LayoutInflater;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.view.ViewGroup;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><br><span class="hljs-keyword">import</span> androidx.annotation.NonNull;<br><span class="hljs-keyword">import</span> androidx.annotation.Nullable;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">PictureAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;Picture&gt; &#123;<br>    <span class="hljs-keyword">private</span> Context context;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;Picture&gt; list;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">PictureAdapter</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> Context context, ArrayList&lt;Picture&gt; list)</span> &#123;<br>        <span class="hljs-built_in">super</span>(context, android.R.layout.simple_list_item_1, list);<br>        <span class="hljs-built_in">this</span>.context = context;<br>        <span class="hljs-built_in">this</span>.list = list;<br>    &#125;<br><br>    <span class="hljs-meta">@NonNull</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> View <span class="hljs-title function_">getView</span><span class="hljs-params">(<span class="hljs-type">int</span> position, <span class="hljs-meta">@Nullable</span> View convertView, <span class="hljs-meta">@NonNull</span> ViewGroup parent)</span> &#123;<br>        View v;<br>        <span class="hljs-keyword">if</span>(convertView == <span class="hljs-literal">null</span>)&#123;<br>            v = LayoutInflater.from(context).inflate(R.layout.row_view, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>);<br>        &#125; <span class="hljs-keyword">else</span>&#123;<br>            v = convertView;<br>        &#125;<br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_iv);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_tv);<br>        <span class="hljs-type">Picture</span> <span class="hljs-variable">picture</span> <span class="hljs-operator">=</span> list.get(position);<br>        iv.setImageResource(picture.getPicId());;<br>        tv.setText(picture.getName());<br>        <span class="hljs-keyword">return</span> v;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.GridView;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><br><span class="hljs-keyword">import</span> java.lang.reflect.Array;<br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    ArrayList&lt;Picture&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> findViewById(R.id.textView);<br>        <span class="hljs-type">GridView</span> <span class="hljs-variable">gv</span> <span class="hljs-operator">=</span> findViewById(R.id.gridview);<br><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">4</span>; i++) &#123;<br>            list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Picture</span>(<span class="hljs-string">&quot;图片1&quot;</span>, R.drawable.image1));<br>            list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Picture</span>(<span class="hljs-string">&quot;图片2&quot;</span>, R.drawable.image2));<br>            list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Picture</span>(<span class="hljs-string">&quot;图片3&quot;</span>, R.drawable.image3));<br>        &#125;<br>        <span class="hljs-type">PictureAdapter</span> <span class="hljs-variable">adapter</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">PictureAdapter</span>(<span class="hljs-built_in">this</span>, list);<br>        gv.setAdapter(adapter);<br>        gv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                <span class="hljs-type">Picture</span> <span class="hljs-variable">picture</span> <span class="hljs-operator">=</span> adapter.getItem(i);<br>                tv.setText(picture.getName());<br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="6-9-实现多个地区多个景区的图片展示"><a href="#6-9-实现多个地区多个景区的图片展示" class="headerlink" title="6.9 实现多个地区多个景区的图片展示"></a>6.9 实现多个地区多个景区的图片展示</h2><hr>
<p><code>实现效果</code><br><img src="https://img-blog.csdnimg.cn/593f6f8917a54dd98418fbbaf437fdf5.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/946d7eff1a564e68b682abc1ead109a6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/eb4bde7bd6fa4b10a01d2ba2ac4410c6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>layout_scenery.xml</code><br><img src="https://img-blog.csdnimg.cn/de1b9db8c31f44eb8a63b7277c59727f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;horizontal&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center_vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/iv_pic&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;120dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;80dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_margin</span>=<span class="hljs-string">&quot;10px&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:scaleType</span>=<span class="hljs-string">&quot;centerCrop&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@drawable/bj_beihai&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/tv_name&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;100dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_weight</span>=<span class="hljs-string">&quot;1&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center_vertical&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;北海&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:textColor</span>=<span class="hljs-string">&quot;@color/teal_200&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">&quot;30sp&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>layout_main.xml</code><br><img src="https://img-blog.csdnimg.cn/95965f00b6d8400c851b4093b7b5b4b5.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/tv_result&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;TextView&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">Spinner</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/spinner&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/imageView&quot;</span></span><br><span class="hljs-tag"></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;180dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;100dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_gravity</span>=<span class="hljs-string">&quot;center&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:scaleType</span>=<span class="hljs-string">&quot;centerCrop&quot;</span></span><br><span class="hljs-tag">        /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ListView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/lv_scenery&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>接下来有两种方法实现最终的效果，一是使用ArrayAdapter，二是继承ArrayAdapter类。<br><strong>方法一：</strong><br><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.content.res.Resources;<br><span class="hljs-keyword">import</span> android.content.res.TypedArray;<br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><span class="hljs-keyword">import</span> android.widget.ListView;<br><span class="hljs-keyword">import</span> android.widget.SimpleAdapter;<br><span class="hljs-keyword">import</span> android.widget.Spinner;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><span class="hljs-keyword">import</span> android.widget.Toast;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><span class="hljs-keyword">import</span> java.util.HashMap;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    <span class="hljs-comment">// 标记 ListView 控件的 Layout内的HashMap键</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">KEY_NAME</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;key_name&quot;</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">String</span> <span class="hljs-variable">KEY_PIC_ID</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;key_pic_id&quot;</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> <span class="hljs-variable">cityId</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;                                 <span class="hljs-comment">// 当前城市id</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br><br>        <span class="hljs-comment">// 创建城市信息、景区信息、图片ID</span><br>        String[] cities = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[]&#123;<span class="hljs-string">&quot;温州&quot;</span>, <span class="hljs-string">&quot;贵阳&quot;</span>, <span class="hljs-string">&quot;杭州&quot;</span>, <span class="hljs-string">&quot;北京&quot;</span>&#125;;<br>        String[][] sceneris= <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[][] &#123;<br>                &#123;<span class="hljs-string">&quot;江心屿&quot;</span>, <span class="hljs-string">&quot;大罗山&quot;</span>, <span class="hljs-string">&quot;洞头区&quot;</span>&#125;,<br>                &#123;<span class="hljs-string">&quot;花果园&quot;</span>, <span class="hljs-string">&quot;甲秀楼&quot;</span>, <span class="hljs-string">&quot;青岩古镇&quot;</span>&#125;,<br>                &#123;<span class="hljs-string">&quot;雷峰塔&quot;</span>, <span class="hljs-string">&quot;千岛湖&quot;</span>, <span class="hljs-string">&quot;西湖&quot;</span>&#125;,<br>                &#123;<span class="hljs-string">&quot;天安门&quot;</span>, <span class="hljs-string">&quot;北海&quot;</span>, <span class="hljs-string">&quot;天坛&quot;</span>&#125;<br>        &#125;;<br>        <span class="hljs-type">int</span>[][] picId = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span> [][]&#123;<br>                &#123;R.drawable.wz_jiangxinyu, R.drawable.wz_daluoshan, R.drawable.wz_dongtouqu&#125;,<br>            &#123;R.drawable.gy_huaguoyuan, R.drawable.gy_jiaxiulou, R.drawable.gy_qingyanguzhen&#125;,<br>            &#123;R.drawable.hz_leifengta, R.drawable.hz_qiandaohu, R.drawable.hz_xihu&#125;,<br>            &#123;R.drawable.bj_tiananmen, R.drawable.bj_beihai, R.drawable.bj_tiantan&#125;<br>        &#125;;<br><br>        <span class="hljs-comment">// 创建选择城市和景区的Adapter</span><br>        ArrayAdapter&lt;String&gt; adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_1, cities);<br>        ArrayList&lt;HashMap&lt;String, Object&gt;&gt; list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br><br>        <span class="hljs-comment">// 获取组件</span><br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> findViewById(R.id.tv_result);         <span class="hljs-comment">// 显示 &quot;城市:景区&quot; 信息</span><br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> findViewById(R.id.imageView);        <span class="hljs-comment">// 显示 景区图片</span><br>        <span class="hljs-type">ListView</span> <span class="hljs-variable">lv</span> <span class="hljs-operator">=</span> findViewById(R.id.lv_scenery);        <span class="hljs-comment">// 选择 景区</span><br>        <span class="hljs-type">Spinner</span> <span class="hljs-variable">sp</span> <span class="hljs-operator">=</span> findViewById(R.id.spinner);            <span class="hljs-comment">// 选择 城市</span><br><br>        sp.setAdapter(adapter);                             <span class="hljs-comment">// 设置spinner对应的adapter 选择城市</span><br><br>        <span class="hljs-comment">// 当选择某个城市时的响应</span><br>        sp.setOnItemSelectedListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemSelectedListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                <span class="hljs-comment">// 更改当前城市的 id</span><br>                cityId = i;<br><br>                <span class="hljs-comment">// 显示城市信息</span><br>                tv.setText(cities[i]);<br><br>                <span class="hljs-comment">// 默认显示该城市第一个景区</span><br>                iv.setImageResource(picId[i][<span class="hljs-number">0</span>]); <span class="hljs-comment">// 默认显示第一个景区</span><br><br>                <span class="hljs-comment">// 根据选择的地点 更新景区选项</span><br>                list.clear();<br>                <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j &lt; sceneris[i].length; ++j)&#123;<br>                    HashMap&lt;String, Object&gt; hashMap = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>                    hashMap.put(KEY_NAME, sceneris[i][j]);<br>                    hashMap.put(KEY_PIC_ID, picId[i][j]);<br>                    list.add(hashMap);<br>                &#125;<br><br>                <span class="hljs-comment">// 创建列表框ListView的Adapter</span><br>                <span class="hljs-type">SimpleAdapter</span> <span class="hljs-variable">ad</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleAdapter</span>(MainActivity.<span class="hljs-built_in">this</span>, list, R.layout.layout_scenery,<br>                        <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[] &#123;KEY_NAME, KEY_PIC_ID&#125;,<br>                        <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span> [] &#123;R.id.tv_name, R.id.iv_pic&#125;);<br>                lv.setAdapter(ad);<br><br>                <span class="hljs-comment">// 当选择列表框的某个景区时的响应</span><br>                lv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>                    <span class="hljs-meta">@Override</span><br>                    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> i, <span class="hljs-type">long</span> l)</span> &#123;<br>                        HashMap&lt;String, Object&gt; hashMap = list.get(i);      <span class="hljs-comment">// 获取该景区的哈希表信息</span><br>                        <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> (String) hashMap.get(KEY_NAME);       <span class="hljs-comment">// 获取景区名称</span><br>                        iv.setImageResource(picId[cityId][i]);              <span class="hljs-comment">// 设置景区图片</span><br>                        tv.setText(cities[cityId] + <span class="hljs-string">&quot;:&quot;</span> + name);            <span class="hljs-comment">// 更新提示文本</span><br>                    &#125;<br>                &#125;);<br>            &#125;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onNothingSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView)</span> &#123;&#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>方法二（layout都是一样的）：</strong><br><img src="https://img-blog.csdnimg.cn/c0626e4c68764ff5819f03482a1e4b3f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li><code>Adpter层</code>负责编写继承ArrayAdapter的类，作为ListView组件的适配器</li>
<li><code>Controller层</code>负责数据的交互，包括Spinner（显示城市名）和ListView（显示景区图片及其名称）展示的数据</li>
<li><code>Entity层</code>负责编写存储景区实体的类，包括<code>景区名</code>和<code>景区图片ID</code>两个属性</li>
</ul>
<p><code>Landscape.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Landscape</span> &#123;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> picId;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Landscape</span><span class="hljs-params">(String name, <span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getPicId</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> picId;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setPicId</span><span class="hljs-params">(<span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p><code>LandscapeAdapter.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> android.content.Context;<br><span class="hljs-keyword">import</span> android.view.LayoutInflater;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.view.ViewGroup;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br> <br><span class="hljs-keyword">import</span> androidx.annotation.NonNull;<br><span class="hljs-keyword">import</span> androidx.annotation.Nullable;<br> <br><span class="hljs-keyword">import</span> com.example.R;<br><span class="hljs-keyword">import</span> com.example.entity.Landscape;<br> <br><span class="hljs-keyword">import</span> java.util.ArrayList;<br> <br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LandscapeAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;Landscape&gt; &#123;<br> <br>    <span class="hljs-keyword">private</span> Context context;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;Landscape&gt; list;<br> <br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">LandscapeAdapter</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> Context context, ArrayList&lt;Landscape&gt; list)</span> &#123;<br>        <span class="hljs-built_in">super</span>(context, android.R.layout.simple_list_item_1, list);<br>        <span class="hljs-built_in">this</span>.context = context;<br>        <span class="hljs-built_in">this</span>.list = list;<br>    &#125;<br> <br>    <span class="hljs-meta">@NonNull</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> View <span class="hljs-title function_">getView</span><span class="hljs-params">(<span class="hljs-type">int</span> position, <span class="hljs-meta">@Nullable</span> View convertView, <span class="hljs-meta">@NonNull</span> ViewGroup parent)</span> &#123;<br>        View v;<br>        <span class="hljs-keyword">if</span>(convertView == <span class="hljs-literal">null</span>)<br>            v = LayoutInflater.from(context).inflate(R.layout.layout_row, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>);<br>        <span class="hljs-keyword">else</span><br>            v = convertView;<br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_iv);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_tv);<br>        <span class="hljs-type">Landscape</span> <span class="hljs-variable">landscape</span> <span class="hljs-operator">=</span> list.get(position);<br>        iv.setImageResource(landscape.getPicId());<br>        tv.setText(landscape.getName());<br>        <span class="hljs-keyword">return</span> v;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>LandscapeController.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.example.R;<br><span class="hljs-keyword">import</span> com.example.entity.Landscape;<br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LandscapeController</span> &#123;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;ArrayList&lt;Landscape&gt;&gt; landscapeLists;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;Landscape&gt; curLandscapeList;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;String&gt; cityList;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> curCityPosition;<br>    <span class="hljs-keyword">private</span> String curCityName;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">LandscapeController</span><span class="hljs-params">()</span> &#123;<br>        init();<br>    &#125;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span><span class="hljs-params">()</span>&#123;<br>        landscapeLists = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        cityList = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        ArrayList&lt;Landscape&gt; list_wz = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        list_wz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;大罗山&quot;</span>, R.drawable.wz_daluoshan));<br>        list_wz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;洞头&quot;</span>, R.drawable.wz_dongtou));<br>        list_wz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;江心屿&quot;</span>, R.drawable.wz_jiangxinyu));<br> <br>        ArrayList&lt;Landscape&gt; list_hz = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        list_hz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;西湖&quot;</span>, R.drawable.hz_xihu));<br>        list_hz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;雷峰塔&quot;</span>, R.drawable.hz_leifengta));<br>        list_hz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;千岛湖&quot;</span>, R.drawable.hz_qiandaohu));<br> <br>        ArrayList&lt;Landscape&gt; list_gz = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        list_gz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;千户苗寨&quot;</span>, R.drawable.gz_qianhumiaozhai));<br>        list_gz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;梵净山&quot;</span>, R.drawable.gz_fanjingshan));<br>        list_gz.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;织金洞&quot;</span>, R.drawable.gz_zhijindong));<br> <br>        insert(<span class="hljs-string">&quot;温州&quot;</span>, list_wz);<br>        insert(<span class="hljs-string">&quot;杭州&quot;</span>, list_hz);<br>        insert(<span class="hljs-string">&quot;贵州&quot;</span>, list_gz);<br>    &#125;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">insert</span><span class="hljs-params">(String name, ArrayList&lt;Landscape&gt; landscape)</span>&#123;<br>        <span class="hljs-built_in">this</span>.cityList.add(name);<br>        <span class="hljs-built_in">this</span>.landscapeLists.add(landscape);<br>    &#125;<br> <br>    <span class="hljs-keyword">public</span> ArrayList&lt;Landscape&gt; <span class="hljs-title function_">update</span><span class="hljs-params">(<span class="hljs-type">int</span> position)</span>&#123;<br>        <span class="hljs-built_in">this</span>.curCityPosition = position;<br>        curLandscapeList = landscapeLists.get(position);<br>        curCityName = cityList.get(<span class="hljs-built_in">this</span>.curCityPosition);<br>        <span class="hljs-keyword">return</span> curLandscapeList;<br>    &#125;<br> <br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getCurCityName</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> curCityName;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getCurCityPosition</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> curCityPosition;<br>    &#125;<br>    <span class="hljs-keyword">public</span> ArrayList&lt;Landscape&gt; <span class="hljs-title function_">getCurLandscapeList</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> curLandscapeList;<br>    &#125;<br>    <span class="hljs-keyword">public</span> ArrayList&lt;String&gt; <span class="hljs-title function_">getCityList</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> cityList;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><code>MainActivity.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><span class="hljs-keyword">import</span> android.widget.ListView;<br><span class="hljs-keyword">import</span> android.widget.Spinner;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><span class="hljs-keyword">import</span> com.example.adpter.LandscapeAdapter;<br><span class="hljs-keyword">import</span> com.example.controller.LandscapeController;<br><span class="hljs-keyword">import</span> com.example.entity.Landscape;<br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br>    LandscapeController lc;<br>    TextView tv;<br>    ListView lv;<br>    ImageView iv;<br>    LandscapeAdapter adapter;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br> <br>        <span class="hljs-type">Spinner</span> <span class="hljs-variable">sp</span> <span class="hljs-operator">=</span> findViewById(R.id.main_spinner);<br>        lv = findViewById(R.id.main_lv);<br>        tv = findViewById(R.id.main_tv_name);<br>        iv = findViewById(R.id.main_iv);<br>        lc = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeController</span>();<br>        lv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; parent, View view, <span class="hljs-type">int</span> position, <span class="hljs-type">long</span> id)</span> &#123;<br>                updateListViewItem(position);<br>            &#125;<br>        &#125;);<br>        ArrayAdapter&lt;String&gt; spAdapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;&gt;(<span class="hljs-built_in">this</span>, android.R.layout.simple_list_item_1, lc.getCityList());<br>        sp.setAdapter(spAdapter);<br>        sp.setOnItemSelectedListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemSelectedListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; parent, View view, <span class="hljs-type">int</span> position, <span class="hljs-type">long</span> id)</span> &#123;<br>                updateListView(position);<br>            &#125;<br> <br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onNothingSelected</span><span class="hljs-params">(AdapterView&lt;?&gt; parent)</span> &#123;&#125;<br>        &#125;);<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">updateListViewItem</span><span class="hljs-params">(<span class="hljs-type">int</span> position)</span>&#123;<br>        <span class="hljs-type">Landscape</span> <span class="hljs-variable">landscape</span> <span class="hljs-operator">=</span> adapter.getItem(position);<br>        tv.setText(lc.getCurCityName() + <span class="hljs-string">&quot; : &quot;</span> + landscape.getName());<br>        iv.setImageResource(landscape.getPicId());<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">updateListView</span><span class="hljs-params">(<span class="hljs-type">int</span> position)</span>&#123;<br>        ArrayList&lt;Landscape&gt; list = lc.update(position);<br>        adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeAdapter</span>(<span class="hljs-built_in">this</span>, list);<br>        lv.setAdapter(adapter);<br>        updateListViewItem(<span class="hljs-number">0</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%AE%89%E5%8D%93/" class="category-chain-item">安卓</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Android/">#Android</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo</div>
      <div>http://example.com/2021/10/25/Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年10月25日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/10/26/Android%20_%20%E5%9F%BA%E4%BA%8EMenu%E7%BB%84%E4%BB%B6%E5%AE%9E%E7%8E%B0%E6%99%AF%E5%8C%BA%E7%9A%84%E6%B7%BB%E5%8A%A0%E3%80%81%E4%BF%AE%E6%94%B9%E3%80%81%E5%88%A0%E9%99%A4/" title="Android _ 基于Menu组件实现景区的添加、修改、删除">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Android _ 基于Menu组件实现景区的添加、修改、删除</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/19/Android%20_%20%E7%AC%AC1%E7%AB%A0%20_%20UI%E8%AE%BE%E8%AE%A1%20_%20%E7%9F%A5%E8%AF%86%E7%82%B9%E6%B1%87%E6%80%BB%20_%20+Demo/" title="Android _ 第1章 _ UI设计 _ 知识点汇总 _ +Demo">
                        <span class="hidden-mobile">Android _ 第1章 _ UI设计 _ 知识点汇总 _ +Demo</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
